/*
 * @Author: xinyang3
 * @Date: 2021-07-28 09:03
 * @Descripttion: fuction types
 * @LastEditors: xinyang3
 * @LastEditTime: 2021-07-28 09:29
 */

// 方法的多态性
type Reservation = {};
type Reserve = {
  (from: Date, to: Date, destination: string): Reservation;
  (from: Date, destination: string): Reservation;
  (
    from: Date,
    toOrDestination: Date | string,
    destination?: string
  ): Reservation;
};

type CreateElement = {
  (tag: 'a'): HTMLAnchorElement;
  (tag: 'canvas'): HTMLCanvasElement;
  (tag: 'table'): HTMLTableElement;
  (tag: string): HTMLElement;
};

function createElement(tag: 'a'): HTMLAnchorElement;
function createElement(tag: 'canvas'): HTMLCanvasElement;
function createElement(tag: 'table'): HTMLTableElement;
function createElement(tag: string): HTMLElement {
  return new HTMLDivElement();
}

// 复合类型，function也是对象
type WarnUser = {
  (warning: string): void;
  wasCalled: boolean;
};

// 符合类型的
let warnUser: WarnUser = (warning: string) => {
  if (warnUser.wasCalled) {
    return;
  }
  warnUser.wasCalled = true;
};
warnUser.wasCalled = false;
